<?php

/**
 * Workspace's activites
 * @package uniCRM
 * @version 1.0b
 */

session_start();

// Configuration file
include 'configuration.php';
// Functions
include 'inc/functions.php';

// Checking if user is logged in
if (!CheckSession()):
	header('Location: index.php');
	exit();
endif;

// Include all objects
foreach (glob($configuration['webroot'].'objects/*.php') as $class) { include_once $class; }

// ---------- START: HEAD
include $configuration['webroot'].'inc/head.php';
// ---------- END: HEAD

// ---------- START: MENU
include $configuration['webroot'].'inc/menu.php';
// ---------- END: MENU

// ---------- START: BODY ?>

<div id="title" style="background-image:url('css/img/activities.png')">Mes activités</div>

<?php
if (!isset($_GET['operation'])):
	
	$crmuser = unserialize($_SESSION['current_user']);
	if (empty($_SESSION['search']['myactivities']))
		$params = Array(Array('crmuserId', '=', $crmuser->crmuserId));
	else 
		$params = Array(Array('crmuserId', '=', $crmuser->crmuserId), Array('title', 'LIKE', '%'.$_SESSION['search']['myactivities'].'%'));

	$activity = new Activity();
	$activityList = $activity->GetList($params, 'date'); ?>
	
	<div id="nav">
		<a href="#" id="details" class="nav">Détails</a>
		<span class="separator"></span>
		<a href="myactivities.php?operation=add" id="add" class="nav">Nouveau</a>
		<a href="#" id="delete" class="nav">Supprimer</a>
		<span class="separator"></span>
		<input type="text" id="search-field" class="search-field" value="<?php echo (!empty($_SESSION['search']['myactivities'])) ? $_SESSION['search']['myactivities'] : ''; ?>" />
		<a href="myactivities" id="search" class="search">Rechercher</a>
	</div>
		
	<table cellpadding="0" cellspacing="0" border="0" class="table-objects">
		<tr>
			<th class="check"></th>
			<th>#</th>
			<th>Type</th>
			<th>Priorité</th>
			<th>Contact</th>
			<th>Propriétaire</th>
			<th>Date</th>
			<th>Sujet</th>
		</tr>
		<?php
		if (count($activityList) > 0):
			foreach ($activityList as $activity):
				$contact = $activity->GetContact();
				$crmuser = $activity->GetCrmuser();
				$crmcontact = $crmuser->GetContact();
				$date = explode('-', substr($activity->date, 0, 10));
				$date = $date[2].'/'.$date[1].'/'.$date[0].' à '.substr($activity->date, 11, 5);
				
				$res  = '<tr>';
				$res .= '<td class="check"><input type="checkbox" value="'.$activity->activityId.'" /></td>';
				$res .= '<td>'.$activity->activityId.'</td>';
				$res .= '<td>'.$activityType[$activity->type].'</td>';
				$res .= '<td>'.$priority[$activity->priority].'</td>';
				$res .= '<td><a href="contacts.php?id='.$contact->contactId.'&operation=edit">'.$contact->lastname.' '.$contact->firstname.'</a></td>';
				$res .= '<td><a href="crmusers.php?id='.$crmuser->crmuserId.'&operation=edit">'.$crmcontact->lastname.' '.$crmcontact->firstname.'</a></td>';
				$res .= '<td>'.$date.'</td>';
				$res .= '<td>'.((strlen($activity->title) > 50) ? substr($activity->title).'...' : $activity->title).'</td>';
				$res .= '</tr>';
				echo $res;
			endforeach;
		else:
			echo '<tr><td colspan="8" class="ui-state-error">Aucune donnée</td></tr>';
		endif;
		?>
	</table>
	
	<div id="sub-nav">
		<a href="#" class="selectall">Tout cocher</a>
		<a href="#" class="selectnone">Tout décocher</a>
	</div>
	
<?php
else:
	$activity = new Activity();
	$contact = new Contact();
	$crmuser = unserialize($_SESSION['current_user']);
	$crmcontact = $crmuser->GetContact();
	$activity->crmuserId = $crmuser->crmuserId;
	$date = '';
	$hour = date('H:i');
	
	// In case we are in an update state
	if (($_GET['operation'] == 'edit') && isset($_GET['id'])):
		$activity->Get($_GET['id']);
		$contact = $activity->GetContact();
		$crmuser = $activity->GetCrmuser();
		$crmcontact = $crmuser->GetContact();
		// Format purposes
		if (strlen($activity->date) > 0):
			$date = explode('-', substr($activity->date, 0, 10));
			$date = $date[2].'/'.$date[1].'/'.$date[0];
			$hour = substr($activity->date, 11, 5);
		endif;
	endif;

	include 'forms/activity.php';

endif;
// ---------- END: BODY

// ---------- START: FOOT
include $configuration['webroot'].'inc/foot.php';
// ---------- END: FOOT

// ---------- JAVASCRIPTS AND END
include $configuration['webroot'].'inc/js.php'; ?>
<script type="text/javascript" src="js/activities.js"></script>

</body>
</html>